package com.weipeep.sys.controller;


import java.util.Arrays;
import java.util.List;

import com.weipeep.common.base.BaseController;
import com.weipeep.common.utils.IdGenerator;
import com.weipeep.sys.domain.DemandDO;
import com.weipeep.sys.domain.XunJiaDO;
import com.weipeep.sys.service.DemandService;
import com.weipeep.sys.service.XunJiaService;
import com.weipeep.sys.vo.BaojiaVo;
import com.weipeep.sys.vo.ReceiveBaojiaQuery;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.weipeep.sys.domain.BaoJiaDO;
import com.weipeep.sys.service.BaoJiaService;
import com.weipeep.common.utils.Result;

import javax.annotation.Resource;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2018-04-21 00:42:43 | wangyun</small>
 */
@Controller
@RequestMapping("/sys/baoJia")
public class BaoJiaController extends BaseController{
	@Autowired
	private BaoJiaService baoJiaService;
	@Resource
	private DemandService demandService;
	@Resource
	private XunJiaService xunJiaService;
	@GetMapping()
//	@RequiresPermissions("sys:baoJia:baoJia")
	String BaoJia(){
	    return "sys/baojia/baoJia";
	}
	
	@ResponseBody
	@GetMapping("/list")
//	@RequiresPermissions("sys:baoJia:baoJia")
	public Result<Page<BaojiaVo>> list(Integer pageNumber, Integer pageSize, BaoJiaDO baoJiaDTO){
		// 查询列表数据
		Page<BaojiaVo> page = new Page<>(pageNumber, pageSize);
		ReceiveBaojiaQuery query = new ReceiveBaojiaQuery();
		query.setBaojiaUserId(getUserId());
		if (baoJiaDTO.getModel()!=null && baoJiaDTO.getModel().trim().equals("")){
			query.setModel(null);
		}else {
			query.setModel("%"+baoJiaDTO.getModel()+"%");
		}
		query.setLimit(page.getLimit());
		query.setOffset(page.getOffset());
		List<BaojiaVo> baoJiaDOList= baoJiaService.myBaojia(query);
		int total = baoJiaService.myBaojiaCount(query);
		page.setRecords(baoJiaDOList);
		page.setTotal(total);
		return Result.ok(page);
	}

    @ResponseBody
    @GetMapping("/receive/baojia")
//	@RequiresPermissions("sys:baoJia:baoJia")
    public Result<Page<BaojiaVo>> receiveBaojia(Integer pageNumber, Integer pageSize, BaoJiaDO baoJiaDTO){
        // 查询列表数据
		Page<BaojiaVo> page = new Page<>(pageNumber, pageSize);
		ReceiveBaojiaQuery query = new ReceiveBaojiaQuery();
        query.setXunjiaUserId(getUserId());
        if (baoJiaDTO.getModel()!=null && baoJiaDTO.getModel().trim().equals("")){
            query.setModel(null);
        }else {
        	query.setModel("%"+baoJiaDTO.getModel()+"%");
		}
		query.setLimit(page.getLimit());
        query.setOffset(page.getOffset());
        List<BaojiaVo> baoJiaDOList= baoJiaService.receiveBaojia(query);
        int total = baoJiaService.receiveBaojiaCount(query);
		page.setRecords(baoJiaDOList);
		page.setTotal(total);
        return Result.ok(page);
    }

	@GetMapping("/add")
//	@RequiresPermissions("sys:baoJia:add")
	String add(){
	    return "sys/baoJia/add";
	}

	@GetMapping("/edit/{id}")
//	@RequiresPermissions("sys:baoJia:edit")
	String edit(@PathVariable("id") Long id,Model model){
		BaoJiaDO baoJia = baoJiaService.selectById(id);
		model.addAttribute("baoJia", baoJia);
	    return "sys/baojia/edit";
	}

	/***
	 * 报价编辑页面
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping("/baojia/{id}")
	String baojia(@PathVariable("id") Long id,Model model){
		DemandDO demand = demandService.selectById(id);
		model.addAttribute("demand", demand);
		return "sys/baojia/baojia";
	}
	/**
	 * 报价保存页面
	 */
	@ResponseBody
	@PostMapping("/baojia")
//	@RequiresPermissions("sys:baoJia:add")
	public Result<String> save( BaoJiaDO baoJia){
		Long userId = getUserId();
		baoJia.setUserId(userId);
		if(baoJiaService.baojia(baoJia)){
			return Result.ok();
		}
		return Result.fail();
	}

	/**
	 * 报价保存页面
	 */
	@ResponseBody
	@PostMapping("/baojiatwo")
	public Result<String> saveTwo( BaoJiaDO baoJia){
		Long userId = getUserId();
		baoJia.setUserId(userId);
		if(baoJiaService.baojia(baoJia)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
//	@RequiresPermissions("sys:baoJia:edit")
	public Result<String>  update( BaoJiaDO baoJia){
		baoJiaService.updateById(baoJia);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
//	@RequiresPermissions("sys:baoJia:remove")
	public Result<String>  remove( Long id){
		if(baoJiaService.deleteById(id)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
//	@RequiresPermissions("sys:baoJia:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		baoJiaService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
